package com.skirmisher.video.controller;

import com.aliyuncs.exceptions.ClientException;
import com.skirmisher.video.entity.frame.ResponseModel;
import com.skirmisher.video.entity.video.*;
import com.skirmisher.video.service.AliVideoService;
import com.skirmisher.video.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName AliVideoController
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/11/8 7:07 下午
 * @Version 1.0
 */
@RestController
@RequestMapping("/video")
public class AliVideoController {

    @Autowired
    private AliVideoService aliVideoService;

    @PostMapping(value = "/startDevice")
    public ResponseModel startDevice(String deviceId){

        ResponseModel responseModel = new ResponseModel();

       if (StringUtils.isEmpty(deviceId)){
           responseModel.setCode(201);
           responseModel.setMessage("无设备id");
           return responseModel;
       }

        try {
            aliVideoService.startDevice(deviceId);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("开启失败");
        }

        return responseModel;

    }

    @PostMapping(value = "/stopDevice")
    public ResponseModel stopDevice(String deviceId){

        ResponseModel responseModel = new ResponseModel();

        if (StringUtils.isEmpty(deviceId)){
            responseModel.setCode(201);
            responseModel.setMessage("无设备id");
            return responseModel;
        }

        try {
            aliVideoService.stopDevice(deviceId);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("开启失败");
        }

        return responseModel;

    }

    @GetMapping(value = "/getDeviceUrl")
    public ResponseModel getDeviceUrl(String deviceId){

        ResponseModel responseModel = new ResponseModel();

        if (StringUtils.isEmpty(deviceId)){
            responseModel.setCode(201);
            responseModel.setMessage("无设备id");
            return responseModel;
        }

        try {
           String url = aliVideoService.getDeviceUrl(deviceId);
           responseModel.setData(url);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("获取url失败");
        }

        return responseModel;

    }

    /**
     * @Author hanwangxin
     * @Description //TODO 获取设备列表 * @Date 8:56 下午 2020/11/8
     * @Param []
     * @return com.skirmisher.video.entity.frame.ResponseModel
     **/
    @GetMapping(value = "/getDevice")
    public ResponseModel getDevice(DevicePageQueryEntity pageQuery){

        ResponseModel responseModel = new ResponseModel();

        Integer startRow = PageUtils.getStartRow(pageQuery.getPage(), pageQuery.getRows());
        if (startRow == null){
            responseModel.setCode(201);
            responseModel.setMessage("分页参数有误");
            return responseModel;
        }

        pageQuery.setStartRows(startRow);

        aliVideoService.getDevice(pageQuery);
        responseModel.setDataDataGrid(pageQuery.getDevicelist(), pageQuery.getCount());

        return responseModel;
    }

    /**
     * @Author hanwangxin
     * @Description //TODO 同步信息 * @Date 8:57 下午 2020/11/8
     * @Param []
     * @return com.skirmisher.video.entity.frame.ResponseModel
     **/
    @GetMapping(value = "/download")
    public ResponseModel download(){

        ResponseModel responseModel = new ResponseModel();
        try {
            aliVideoService.download();
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("更新失败");
        }

        return responseModel;
    }

    @PostMapping(value = "/snapshot")
    public ResponseModel snapshot(String deviceId){
        ResponseModel responseModel = new ResponseModel();
        try {
            String imagUrl = aliVideoService.snapshot(deviceId);
            responseModel.setData(imagUrl);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("请先开启设备");

        }

        return responseModel;
    }

    @PostMapping("/recordCallBack")
    public ResponseModel recordCallBack(@RequestBody RecordCallBackEntity callBack){
        ResponseModel responseModel = new ResponseModel();

        System.out.println("--------阿里录制回调"+"------"+callBack.getStream()+"------"+callBack.getUri()+"===="+callBack.getDuration()
        +"-----"+callBack.getStart_time()+"======"+callBack.getStop_time());
        aliVideoService.recordCallBack(callBack);
        return responseModel;
    }

    @PostMapping(value = "/startRecordStream")
    public ResponseModel startRecordStream(String deviceId){
        ResponseModel responseModel = new ResponseModel();
        try {
            String imagUrl = aliVideoService.startRecordStream(deviceId);
            responseModel.setData(imagUrl);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("请先开启设备");

        }

        return responseModel;
    }

    @PostMapping(value = "/stopRecordStream")
    public ResponseModel stopRecordStream(String deviceId){
        ResponseModel responseModel = new ResponseModel();
        try {
            String imagUrl = aliVideoService.stopRecordStream(deviceId);
            responseModel.setData(imagUrl);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("关闭失败设备");

        }

        return responseModel;
    }
    
    /**
     * @Author hanwangxin
     * @Description //TODO  * @Date 9:35 下午 2020/11/9
     * @Param []
     * @return com.skirmisher.video.entity.frame.ResponseModel
     **/
    @GetMapping(value = "/getRecord")
    public ResponseModel getRecord(RecordPageQueryEntity page ){
        ResponseModel responseModel = new ResponseModel();

        Integer startRow = PageUtils.getStartRow(page.getPage(), page.getRows());
        if (startRow == null){
            responseModel.setCode(201);
            responseModel.setMessage("分页参数有误");
            return responseModel;
        }

        page.setStartRows(startRow);

        aliVideoService.getRecord(page);
        responseModel.setDataDataGrid(page.getRecordList(), page.getCount());

        return responseModel;
    }

    @PostMapping(value = "/device2Project")
    public ResponseModel device2Project(@RequestBody Device2ProjectEntity device2Project){
        ResponseModel responseModel = new ResponseModel();
        //参数校验
        String deviceId = device2Project.getDeviceId();
        String projectId = device2Project.getProjectId();
        if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(projectId)){
            responseModel.setCode(201);
            responseModel.setMessage("缺少设备或者项目id");
            return responseModel;
        }
        aliVideoService.device2Project(device2Project);


        return responseModel;

    }

    @PostMapping("/saveVideo2Project")
    public  ResponseModel saveVideo2Project(@RequestBody Video2ProjectEntity video2Project){

        ResponseModel responseModel = new ResponseModel();
        //参数校验
        String videId = video2Project.getVideId();
        if (StringUtils.isEmpty(videId)){
            responseModel.setCode(201);
            responseModel.setMessage("缺少设备或者项目id");
            return responseModel;
        }
        aliVideoService.saveVideo2Project(video2Project);


        return responseModel;
    }

    @PostMapping(value = "/creatDevice")
    public ResponseModel creatDevice(@RequestBody NationalDeviceEntity device){
        ResponseModel responseModel = new ResponseModel();
        //参数校验
        String groupId = device.getGroupId();
        String deviceName = device.getDeviceName();
        String userName = device.getUserName();
        String password = device.getPassword();

        if (StringUtils.isEmpty(groupId)||StringUtils.isEmpty(deviceName)||StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            responseModel.setCode(201);
            responseModel.setMessage("缺少参数");
            return responseModel;
        }
        try {
            aliVideoService.creatDevice(device);
        } catch (ClientException e) {
            e.printStackTrace();
            responseModel.setCode(202);
            responseModel.setMessage("新增失败");
        }


        return responseModel;
    }

}
